# Figure_A1.R
# John G. Bullock (john@johnbullock.org)
# 2016 August 02

# This file creates Figure A1 in 
#   Gertler, Aaron L., and John G. Bullock.  2016.  "Reference Rot: An 
#   Emerging Threat to Transparency in Political Science."  PS: Political 
#   Science and Politics.
#
# Figure A1 is a mosaic plot: { referent type } * { institutional or personal resource }.
# "Referent type" is "reproducibility," "database," or "bibliographic source."

library(grid)
library(vcd)   # for mosaic()
source('URL_dataCoding.R')



###############################################################################
# SET UP FIGURE PARAMETERS
##############################################################################
filenameStem         <- 'Figure_A1'  
PDFtitle             <- filenameStem
PS_width             <- 9
PS_height            <- 9
postscriptBackground <- 'transparent'
baseCexSize          <- 1.0
xLabSize             <-  .80 * baseCexSize
yLabSize             <-  .80 * baseCexSize



###############################################################################
# PREPARE LABELS
##############################################################################
tab       <- with(URL_data, table(referent, URL_type))
tabProps  <- round(
  x      = prop.table(table(URL_data$referent, URL_data$URL_type)),  
  digits = 2)
tabLabels <- apply(tabProps, 2, function (x) sub('0\\.0?(\\d\\d?)', '\\1%', x))
tabLabels <- ifelse(tabProps < .04, NA, tabLabels)
dimnames(tabLabels) <- dimnames(tab)



##############################################################################
# DRAW THE FIGURE
##############################################################################
# For more on the vierwport setup, see http://www.jstatsoft.org/v17/i03/paper 
# (Figure 17) and http://stackoverflow.com/questions/29571691.
setwd('figureOutput/')
pdf(
  file   = paste0(filenameStem, '.pdf'), 
  width  = PS_width, 
  height = PS_height, 
  paper  = "special", 
  title  = PDFtitle,
  bg     = postscriptBackground)
sizedViewport <- viewport(
  width  = unit(5.09, "in"),
  height = unit(4.92, "in"))  # creates height of 4.00"
pushViewport(sizedViewport)
tmp <- mosaic(  
  # URL_type ~ referent, data = URL_data,
  x              = tab,
  split_vertical = FALSE,
  spacing        = spacing_increase,
  labeling_args  = list(
    gp_labels    = gpar(col="black", cex = xLabSize),  # factor label names
    gp_varnames  = gpar(font = 1),
    offset_varnames = c(0, 0, 0, 1),  # push LHS variable name ("Type of Referent") further to left
    set_labels   = list(
      Reproducibility        = "Reproducibility",
      Database               = "aaa",
      "Bibliographic source" = "xxx",
      "Institutional site"   = "yyy",
      "Personal site"        = "zzz"),
    set_varnames = c(
      referent = "Type of Referent", 
      URL_type = "")),
  pop            = FALSE,  # pop viewport -- necessary for labeling_cells()
  highlighting = "URL_type",
  highlighting_fill = grey.colors(3)[c(3,2)],
  newpage = FALSE
  #gp = shading_binary
  #shade = FALSE
)
labeling_cells(
  text   = tabLabels, 
  margin = 0,
  gp     = gpar(cex = xLabSize))(tab)
seekViewport("margin_left")
grid.segments(.175, .04, .175, .96)
dev.off()